package com.example.james.litepaltest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.create_database);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LitePal.getDatabase();
//                Connector.getDatabase();
            }
        });

        Button addButton = findViewById(R.id.add_data);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Book book = new Book();
                book.setAuthor("Dan Brown");
                book.setName("The Da Vinci Code");
                book.setPages(454);
                book.setPrice(16.96);
                book.setPress("Unknown");
                book.save();
                Book book1 = new Book();
                book1.setPrice(19.86);
                book1.setAuthor("Dan Brown");
                book1.setName("The Lost Symbol");
                book1.setPages(684);
                book1.setPress("Unknown");
                book1.save();
            }
        });

        Button updateButton = findViewById(R.id.update_data);
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Book book = new Book();
                book.setPress("Anchor");
                book.setPrice(14.95);
                book.updateAll("name=?", "The Da Vinci Code");
            }
        });

        Button deleteButton = findViewById(R.id.delete_data);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataSupport.deleteAll(Book.class, "price < ?", "15");
            }
        });

        Button queryButton = findViewById(R.id.query_data);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Book> books = DataSupport.findAll(Book.class);
                for (Book book : books){
                    Log.i(TAG, "-----------------------------");
                    Log.i(TAG, "Id: " + book.getId());
                    Log.i(TAG, "Name: " + book.getName());
                    Log.i(TAG, "Price: " + book.getPrice());
                    Log.i(TAG, "Pages: " + book.getPages());
                    Log.i(TAG, "Author: " + book.getAuthor());
                    Log.i(TAG, "Press: " + book.getPress());
                    Log.i(TAG, "=============================");
                }
            }
        });
    }
}
